package actions;

import java.sql.SQLException;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.UserModel;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import db.DBModel;

public class DeleteUserAction extends Action{
	public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request,HttpServletResponse response){
		int id=Integer.parseInt(request.getParameter("id"));
		UserModel u=new UserModel();
		u.setId(id);
		try {
			DBModel db=new DBModel();
			u.setUsername(db.getUserFromId(id).getUsername());
			db.deleteUser(id);
			db.close();
			request.setAttribute("userDeleted",u);
			return mapping.findForward("success");
			
		} catch (Exception e){
			e.printStackTrace();
			request.setAttribute("error", e.toString());
			return mapping.findForward("fail");
		}	
	}
}
